package com.craftcomputing.client.Listeners;

import com.craftcomputing.client.Listeners.SearchResultListeners.*;
import com.craftcomputing.client.MainPage;
import com.craftcomputing.client.Entry.IntroEntry;
import com.craftcomputing.client.Header.*;
import com.craftcomputing.client.SearchResult.*;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.*;

public class HeaderListeners {
	static public class HeaderClickListener implements ClickListener
	{
		public void onClick(Widget w){
			MainPage.setMainPanel(new IntroEntry());
		}
		
	}

	static public class TabLabel extends Label{
		private String searchString;
		public TabLabel(String title, String searchString){
			super(title);
			this.searchString = searchString;
		}
		private String SearchText(){return searchString;} 
	}
	static public abstract class TabMouseListener implements MouseListener{
		protected TabLabel label;
		private String style;
		protected HeaderTab headerTab;
		public TabMouseListener(HeaderTab headerTab, TabLabel label, String style){
			this.headerTab = headerTab;
			this.label = label;
			this.style = style;
		}
		public void onMouseUp(final Widget sender, final int x, final int y) {}
		public void onMouseMove(final Widget sender, final int x, final int y) {}
		public void onMouseEnter(final Widget sender) {
			//label.setStyleName(style);
			if (headerTab.getCurrent() != style)
				label.setStyleName("navTab-over");
		}
		public void onMouseLeave(final Widget sender) {
			if (headerTab.getCurrent() != style)
				label.setStyleName("navTab");
				//label.setStyleName("gwt-Label");
		}
	}
	static public class WorkshopTabMouseListener extends TabMouseListener{
		public WorkshopTabMouseListener(HeaderTab headerTab, TabLabel label, String style){
			super(headerTab, label, style);
		}
		public void onMouseDown(final Widget sender, final int x, final int y) {
			headerTab.setCurrent("workshop");
			label.setStyleName("navTab-clicked");			
			WorkshopSearchResult workshop = new WorkshopSearchResult(true);
			workshop.setStyleName("gwt-SearchResultsPanel");
  		    HTTPRequest.asyncGet("/python/workshops/count", new JSONCountResponseTextHandler(workshop));		        	  
			HTTPRequest.asyncGet(label.SearchText(), 
					new JSONSearchResponseTextHandler(workshop, headerTab.getTextBox().getText()));
		}			
	}
	static public class CodeTabMouseListener extends TabMouseListener{
		public CodeTabMouseListener(HeaderTab headerTab, TabLabel label, String style){
			super(headerTab, label, style);
		}
		public void onMouseDown(final Widget sender, final int x, final int y) {		
			headerTab.setCurrent("code");
			label.setStyleName("navTab-clicked");
			CodeSearchResult code = new CodeSearchResult(true);
			code.setStyleName("gwt-SearchResultsPanel");
  		    HTTPRequest.asyncGet("/python/codesamples/count", new JSONCountResponseTextHandler(code));		        	  
			HTTPRequest.asyncGet(label.SearchText(), 
					new JSONSearchResponseTextHandler(code, headerTab.getTextBox().getText()));
		}			
	}
	static public class MaterialsTabMouseListener extends TabMouseListener{
		public MaterialsTabMouseListener(HeaderTab headerTab, TabLabel label, String style){
			super(headerTab, label, style);
		}
		public void onMouseDown(final Widget sender, final int x, final int y) {		
			headerTab.setCurrent("materials");
			label.setStyleName("navTab-clicked");
			MaterialSearchResult material = new MaterialSearchResult(true);
			material.setStyleName("gwt-SearchResultsPanel");
  		    HTTPRequest.asyncGet("/python/materials/count", new JSONCountResponseTextHandler(material));		        	  
			HTTPRequest.asyncGet(label.SearchText(), 
					new JSONSearchResponseTextHandler(material, headerTab.getTextBox().getText()));
		}			
	}
	static public class ThemesTabMouseListener extends TabMouseListener{
		public ThemesTabMouseListener(HeaderTab headerTab, TabLabel label, String style){
			super(headerTab, label, style);
		}
		public void onMouseDown(final Widget sender, final int x, final int y) {		
			headerTab.setCurrent("themes");
			label.setStyleName("navTab-clicked");
			ThemeSearchResult theme = new ThemeSearchResult(true);
			theme.setStyleName("gwt-SearchResultsPanel");
  		    HTTPRequest.asyncGet("/python/themes/count", new JSONCountResponseTextHandler(theme));		        	  
			HTTPRequest.asyncGet(label.SearchText(), 
					new JSONSearchResponseTextHandler(theme, headerTab.getTextBox().getText()));
		}			
	}
}
